#pragma once
#include "mysql.hpp"
#include "chat_session_member.hxx"
#include "chat_session_member-odb.hxx"

namespace suye
{
class ChatSessionMemberTable {
public:
    using ptr = std::shared_ptr<ChatSessionMemberTable>;
    ChatSessionMemberTable(const std::shared_ptr<odb::core::database>& db): _db(db) {}

    // 新增单个会话成员
    bool append(ChatSessionMember& csm) 
    {
        try {
            odb::transaction trans(_db->begin());
            _db->persist(csm);
            trans.commit();
        } catch(std::exception& e) {
            LOG_ERROR("新增单个会话成员失败：{}-{}:{}！", csm.session_id(), csm.user_id(), e.what());
            return false;
        }
        return true;
    }

    // 新增多个会话成员
    bool append(std::vector<ChatSessionMember>& csm_list) 
    {
        try {
            odb::transaction trans(_db->begin());
            for (auto& csm: csm_list) {
                _db->persist(csm);
            }
            trans.commit();
        } catch(std::exception& e) {
            LOG_ERROR("新增多个会话成员失败：{}-{}:{}！", csm_list[0].session_id(), csm_list.size(), e.what());
            return false;
        }
        return true;
    }

    // 删除指定会话中的指定成员
    bool remove(ChatSessionMember& csm) 
    {
        try {
            odb::transaction trans(_db->begin());
            typedef odb::query<ChatSessionMember> query;
            _db->erase_query<ChatSessionMember>(query::session_id == csm.session_id() 
                                                && query::user_id == csm.user_id());
            trans.commit();
        } catch(std::exception& e) {
            LOG_ERROR("删除指定会话中的指定成员失败：{}-{}:{}！", csm.session_id(), csm.user_id(), e.what());
            return false;
        }
        return true;
    }

    // 删除指定会话中的全部成员
    bool remove(const std::string& ssid)
    {
        try {
            odb::transaction trans(_db->begin());
            typedef odb::query<ChatSessionMember> query;
            _db->erase_query<ChatSessionMember>(query::session_id == ssid);
            trans.commit();
        } catch(std::exception& e) {
            LOG_ERROR("删除指定会话中的全部成员失败：{}:{}！", ssid, e.what());
            return false;
        }
        return true;
    }

    // 获取会话中的全部成员
    std::vector<std::string> members(const std::string& ssid)
    {
        std::vector<std::string> res;
        try {
            odb::transaction trans(_db->begin());
            typedef odb::query<ChatSessionMember> query;
            typedef odb::result<ChatSessionMember> result;
            result r(_db->query<ChatSessionMember>(query::session_id == ssid));
            for (auto it = r.begin(); it != r.end(); ++it) {
                res.push_back(it->user_id());
            }
            trans.commit();
        } catch(std::exception& e) {
            LOG_ERROR("获取会话中的全部成员失败：{}-{}！", ssid, e.what());
        }
        return res;
    }
   
private:
    std::shared_ptr<odb::core::database> _db;
};
}